package com.gdcontrol.desktop.visao.consulta;

import com.gdcontrol.desktop.controle.ControlePadrao;
import com.gdcontrol.desktop.controle.consulta.ControleConsultaEvento;
import com.gdcontrol.desktop.util.tablemodel.EventoTableModel;
import com.gdcontrol.entidade.Evento;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author gustavo
 */
public class VisaoConsultaEvento extends VisaoConsultaPadrao {

    private static final String OPCAO_ID = "ID";
    private static final String OPCAO_DATA_INICIO = "Data Início";
    private static final String OPCAO_DATA_FIM = "Data Fim";
    private static final String OPCAO_HORA = "Hora";
    private static final String OPCAO_VALOR = "Valor";
    private static final String OPCAO_TIPO = "Tipo Evento";
    
    /**
     * Creates new form VisaoConsultaEvento
     */
    public VisaoConsultaEvento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        carregaEventos();
        getOpcoesPesquisa();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        edPesquisa = new javax.swing.JTextField();
        cbCampoPesquisa = new javax.swing.JComboBox();
        tbEvento = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btPesquisar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta Eventos");

        jLabel1.setText("Campo Pesquisa:");

        jLabel2.setText("Pesquisa:");

        edPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                edPesquisaKeyPressed(evt);
            }
        });

        jTable1.setModel(tableModelEvento);
        tbEvento.setViewportView(jTable1);

        btPesquisar.setText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(cbCampoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(edPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPesquisar))
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(tbEvento, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbCampoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tbEvento, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        if(!edPesquisa.getText().isEmpty()){
            tableModelEvento.limpar();
            String pesquisa = (String) cbCampoPesquisa.getSelectedItem();
            switch(pesquisa){
                case OPCAO_ID:
                    try{
                        filtraId(Integer.parseInt(edPesquisa.getText()));
                    }catch(NumberFormatException e){}
                    break;
                case OPCAO_DATA_INICIO:
                    filtraDataInicio(edPesquisa.getText());
                    break;  
                case OPCAO_DATA_FIM:
                    filtraDataFim(edPesquisa.getText());
                    break;  
                case OPCAO_HORA:
                    filtraHora(edPesquisa.getText());
                    break;  
                case OPCAO_TIPO:
                    filtraNomeTipo(edPesquisa.getText());
                    break;  
                case OPCAO_VALOR:
                    try{
                        filtraValor(Integer.parseInt(edPesquisa.getText()));
                    }catch(NumberFormatException e){}
            }
            tableModelEvento.fireTableDataChanged();
        }else{
            carregaEventos();
        }
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void edPesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_edPesquisaKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            btPesquisarActionPerformed(null);
        }
    }//GEN-LAST:event_edPesquisaKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VisaoConsultaEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VisaoConsultaEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VisaoConsultaEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VisaoConsultaEvento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VisaoConsultaEvento dialog = new VisaoConsultaEvento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btPesquisar;
    private javax.swing.JComboBox cbCampoPesquisa;
    private javax.swing.JTextField edPesquisa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTable jTable1;
    private javax.swing.JScrollPane tbEvento;
    // End of variables declaration//GEN-END:variables

    ControleConsultaEvento controle = new ControleConsultaEvento();
    EventoTableModel tableModelEvento = new EventoTableModel();
    
    private void carregaEventos() {
        tableModelEvento.limpar();
        for (Evento evento : (List<Evento>) getControle().listar()) {
            tableModelEvento.addEvento(evento);
        }
        tableModelEvento.fireTableDataChanged();
    }
    
    private void filtraId(int id){
        Evento evento = getControle().filtraId(id);
        if(evento != null){
            tableModelEvento.addEvento(evento);
        }
    }
    
    private void filtraDataInicio(String data){
        for (Evento evento : (List<Evento>) getControle().filtraDataInicio(data)) {
            tableModelEvento.addEvento(evento);
        }
    }
    
    private void filtraDataFim(String data){
        for (Evento evento : (List<Evento>) getControle().filtraDataFim(data)) {
            tableModelEvento.addEvento(evento);
        }
    }
    
    private void filtraHora(String hora){
        for (Evento evento : (List<Evento>) getControle().filtraHora(hora)) {
            tableModelEvento.addEvento(evento);
        }
    }
    private void filtraValor(int valor){
        for (Evento evento : (List<Evento>) getControle().filtraValor(valor)) {
            tableModelEvento.addEvento(evento);
        }
    }
    
    private void filtraNomeTipo(String nome){
        for (Evento evento : (List<Evento>) getControle().filtraNomeTipo(nome)) {
            tableModelEvento.addEvento(evento);
        }
    }
    
    @Override
    public void getOpcoesPesquisa() {
        Vector comboBoxItems = new Vector();
        comboBoxItems.add(OPCAO_ID);
        comboBoxItems.add(OPCAO_DATA_INICIO);
        comboBoxItems.add(OPCAO_DATA_FIM);
        comboBoxItems.add(OPCAO_HORA);
        comboBoxItems.add(OPCAO_VALOR);
        comboBoxItems.add(OPCAO_TIPO);
        final DefaultComboBoxModel model = new DefaultComboBoxModel(comboBoxItems);
        this.cbCampoPesquisa.setModel(model);
    }

    @Override
    public ControleConsultaEvento getControle() {
        return this.controle;
    }
}
